package net.anzhuo.controller.db.report;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.entity.PaginationVO;
import net.anzhuo.common.utils.RedisUtil;
import net.anzhuo.entity.GsGonggao;
import net.anzhuo.entity.db.CourtNoticeAnnounce;
import net.anzhuo.entity.db.CourtNoticeAnnounceDetailResponseResult;
import net.anzhuo.entity.db.CourtNoticeAnnounceDetailResult;
import net.anzhuo.entity.db.CourtNoticeAnnounceResult;
import net.anzhuo.entity.db.JudgementDetailResult;
import net.anzhuo.entity.db.JudgementResult;
import net.anzhuo.entity.db.NameKeyNoCollection;
import net.anzhuo.entity.db.gonggao.Paging;
import net.anzhuo.entity.db.gonggao.ResponseResult;
import net.anzhuo.entity.db.syncdata.CourtCaseResult;
import net.anzhuo.entity.db.syncdata.PageUtilsCourtCaseSource;
import net.anzhuo.service.GsBaseService;
import net.anzhuo.service.GsGonggaoService;

@RestController
@Slf4j
public class GonggaoController {

	@Autowired
	private GsGonggaoService gsGonggaoService;
	@Autowired
	private GsBaseService gsBaseService;
	///report/gonggao
	@RequestMapping("/JudgeDocV4/SearchJudgmentDoc")
	public ResponseResult getGonggao(String searchKey,String pageIndex, String pageSize) {
		log.info("===>getGonggao_searchKey="+searchKey+", pageSize="+pageSize);
		long startTime=System.currentTimeMillis();
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "50";
		}
		
		ResponseResult rr = null;
		try {
			rr = (ResponseResult)RedisUtil.get("SearchJudgmentDoc:"+searchKey);
			if (rr != null) {
				
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" SearchJudgmentDoc 执行时间："+excTime+"s");
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		GsGonggao gsGonggaoQuery = new GsGonggao();
		//GsBase base = gsBaseService.getByName(searchKey);
		gsGonggaoQuery.setCoMd5(searchKey);
		gsGonggaoQuery.setPageSize(Integer.parseInt(pageSize));
		PaginationVO<GsGonggao> gonggaoList = gsGonggaoService.getListWithPage(gsGonggaoQuery);
		System.out.println("CompanyName="+searchKey+",gonggaoList="+gonggaoList.getList().size()+",coMd5="+searchKey);
		log.info("CompanyName="+searchKey+",gonggaoList="+gonggaoList.getList().size()+",coMd5="+searchKey);
		ResponseResult responseResult = new ResponseResult();
		responseResult.setStatus("200");
		responseResult.setMessage("查询成功");
		
		
		
		List<CourtCaseResult> resultList = new ArrayList<>();
		CourtCaseResult result = null;
		if (gonggaoList.getList()!=null && gonggaoList.getList().size()>0) {
			for (GsGonggao g : gonggaoList.getList()) {
				if(g.getNoticeType()!=null&&g.getNoticeType().equals("裁判文书"))
				{
				result = new CourtCaseResult();
				result.setCourt(g.getCourt());
				result.setCaseName(g.getCaseName());
				result.setCaseNo(g.getCaseNo());
				result.setCaseType(g.getCaseType());
				result.setTrailRound(g.getTrialRound());
				result.setSubmitDate(g.getNoticeTime());
				result.setUpdateDate(g.getUpdateTime());
				result.setIsProsecutor("true");
				result.setIsDefendant("false");
				result.setCourtYear(g.getCourtYear());
				result.setCaseRole(g.getCaseRole());
				resultList.add(result);
				}
			}
		}
		Paging paging = new Paging();
		if(pageIndex==null||pageIndex.equals(""))
		{
			pageIndex="1";
		}
		if(pageSize==null||pageSize.equals(""))
		{
			pageSize="20";
		}
		paging.setPageIndex(Integer.parseInt(pageIndex));
		paging.setPageSize(Integer.parseInt(pageSize));
		paging.setTotalRecords(resultList.size());
		responseResult.setPaging(paging);
		PageUtilsCourtCaseSource pccs=new PageUtilsCourtCaseSource(resultList,paging.getPageIndex(),paging.getPageSize());
		responseResult.setResult(pccs.result);
		
		try {
			RedisUtil.set("SearchJudgmentDoc:"+searchKey, responseResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" SearchJudgmentDoc 执行时间："+excTime+"s");
		return responseResult;
	}
	
	@RequestMapping("/JudgeDocV4/GetJudgementDetail")
	public JudgementResult GetJudgementDetail(String id, String dtype) {
		log.info("===>getGonggaomingxi_searchKey="+id);
	
		JudgementResult rr = null;
		try {
			rr = (JudgementResult)RedisUtil.get("GetJudgementDetail:"+id);
			if (rr != null) {
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		

		GsGonggao gonggao = gsGonggaoService.getByMd5(id);
		
		JudgementResult responseResult = new JudgementResult();
		responseResult.setStatus("200");
		responseResult.setMessage("查询成功");
		
	    
		JudgementDetailResult result=new JudgementDetailResult();
	    result.setId(id);
	    result.setCaseNo(gonggao.getCaseNo());
	    result.setCaseName(gonggao.getCaseName());
	    result.setCaseReason(gonggao.getCaseReason());
	    result.setCaseType(gonggao.getCaseType());
	    result.setCaseTypeCode(gonggao.getCaseTypeCode());
	    result.setContent(gonggao.getContent());
	    result.setCourt(gonggao.getCourt());
	    result.setCreateDate(gonggao.getCreateDate());
	    List<String> defendantlist=new ArrayList<String>();
	    defendantlist.add(gonggao.getDefendant());
	    result.setDefendantlist(defendantlist);
	    List<String> prosecutorlist=new ArrayList<String>();
	    prosecutorlist.add(gonggao.getProsecutorlist());
	    result.setProsecutorlist(prosecutorlist);
	    List<String> appellorlist=new ArrayList<String>();   
	    appellorlist.add(gonggao.getAppellant());
	    result.setAppellor(appellorlist);
		responseResult.setResult(result);
		
		try {
			RedisUtil.set("GetJudgementDetail:"+id, responseResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return responseResult;
	}
	
	
	@RequestMapping("/CourtNoticeV4/SearchCourtAnnouncement")
	public CourtNoticeAnnounceResult SearchCourtAnnouncement(String companyName,String pageIndex, String pageSize,String dtype) {
		log.info("===>getSearchCourtAnnouncement_searchKey="+companyName+", pageSize="+pageSize);
		long startTime=System.currentTimeMillis();
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "50";
		}
		
		CourtNoticeAnnounceResult rr = null;
		try {
			rr = (CourtNoticeAnnounceResult)RedisUtil.get("SearchCourtAnnouncement:"+companyName);
			if (rr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" SearchCourtAnnouncement 执行时间："+excTime+"s");
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		GsGonggao gsGonggaoQuery = new GsGonggao();
	
		gsGonggaoQuery.setCoMd5(companyName);
		gsGonggaoQuery.setPageSize(Integer.parseInt(pageSize));
		PaginationVO<GsGonggao> gonggaoList = gsGonggaoService.getListWithPage(gsGonggaoQuery);
		System.out.println("keyno="+companyName+",gonggaoList="+gonggaoList.getList().size());
		log.info("keyno="+companyName+",gonggaoList="+gonggaoList.getList().size());
		CourtNoticeAnnounceResult responseResult = new CourtNoticeAnnounceResult();
		responseResult.setStatus("200");
		responseResult.setMessage("查询成功");
		
		Paging paging = new Paging();
		paging.setPageIndex(1);
		paging.setPageSize(Integer.parseInt(pageSize));
		paging.setTotalRecords(gonggaoList.getTotalCount());
		responseResult.setPaging(paging);
		
		List<CourtNoticeAnnounce> list=new ArrayList<CourtNoticeAnnounce>();
		if (gonggaoList.getList()!=null && gonggaoList.getList().size()>0) {
			for (GsGonggao g : gonggaoList.getList()) {
				CourtNoticeAnnounce cna=new CourtNoticeAnnounce();
				cna.setId(g.getMd5());
				cna.setContent(g.getContent());
				cna.setCourt(g.getCourt());
				cna.setCategory(g.getNoticeType());
				cna.setUploadDate(g.getNoticeTime());
				cna.setPublishedDate(g.getNoticeTime());
				cna.setPublishedPage(g.getPublishedPage());
				cna.setParty(g.getParty());
				list.add(cna);
			}	
			}
	
		responseResult.setResult(list);
		
		try {
			RedisUtil.set("SearchCourtAnnouncement:"+companyName, responseResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" SearchCourtAnnouncement 执行时间："+excTime+"s");
		return responseResult;
	}
	
	
	
	@RequestMapping("/CourtNoticeV4/SearchCourtAnnouncementDetail")
	public CourtNoticeAnnounceDetailResponseResult SearchCourtAnnouncementDetail(String id) {
		log.info("===>CourtNoticeV4=SearchCourtAnnouncementDetail"+id);
		
		
		CourtNoticeAnnounceDetailResponseResult rr = null;
		try {
			rr = (CourtNoticeAnnounceDetailResponseResult)RedisUtil.get("report:SearchCourtAnnouncementDetail:"+id);
			if (rr != null) {
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		GsGonggao gsGonggaoQuery = new GsGonggao();
		
		gsGonggaoQuery = gsGonggaoService.getByMd5(id);
		
		CourtNoticeAnnounceDetailResponseResult responseResult = new CourtNoticeAnnounceDetailResponseResult();
		responseResult.setStatus("200");
		responseResult.setMessage("查询成功");
		
		CourtNoticeAnnounceDetailResult cadr=new CourtNoticeAnnounceDetailResult();
		List<NameKeyNoCollection> nknc=new ArrayList<NameKeyNoCollection>();
		
		cadr.setCourt(gsGonggaoQuery.getCourt());
		cadr.setCategory(gsGonggaoQuery.getCaseType());
		cadr.setContent(gsGonggaoQuery.getContent());
		cadr.setParty(gsGonggaoQuery.getParty());
		cadr.setProvince(gsGonggaoQuery.getProvince());
		cadr.setSubmitDate(gsGonggaoQuery.getSubmitDate());
		cadr.setPublishDate(gsGonggaoQuery.getNoticeTime());
		cadr.setParty(gsGonggaoQuery.getParty());
		NameKeyNoCollection nkn=new NameKeyNoCollection();
		nkn.setKeyNo("");
		nkn.setName(gsGonggaoQuery.getDefendantlist());
		nknc.add(nkn);
		cadr.setNameKeyNoCollection(nknc);
		responseResult.setResult(cadr);
		
		try {
			RedisUtil.set("report:SearchCourtAnnouncementDetail:"+id, responseResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return responseResult;
	}
	
	
}
